/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TestSwing.java
 *
 * Created on 30/08/2012, 13:50:28
 */
package desvswing_12;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Alexandre
 */
public class TestSwing extends javax.swing.JDialog {

    /** Creates new form TestSwing */
    public TestSwing(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        initMyComponents();
    }

    private void initMyComponents() {
        DefaultTableModel dtm = (DefaultTableModel) jTable1.getModel();
        jTable1.addMouseListener(new MouseListener() {

            @Override
            public void mouseClicked(MouseEvent e) {
                //dentro deste metodo pego a coluna e a linha e verifico o conteudo dela
                //apenas quando for direfente de null, ou seja, depois de digitada alguma
                //informaçao dentro da tabela exibir uma janela informando o valor inserido.
                JTable table = (JTable) e.getSource();
                int row = table.rowAtPoint(e.getPoint());
                int col = table.columnAtPoint(e.getPoint());
                if (table.getValueAt(row, col) != null) {
                    JOptionPane.showMessageDialog(null, "The Value changed is: " + table.getValueAt(row, col));
                }
            }

            @Override
            public void mousePressed(MouseEvent e) {
                System.out.println("Mouse Pressed!!: " + e.getComponent());                                                                                    
            }

            @Override
            public void mouseReleased(MouseEvent e) {
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                /*int a = 10, b = 20;
                System.out.println("Mouse Entered!!!!!!!!");
                JOptionPane.showMessageDialog(null, a + b);*/
            }

            @Override
            public void mouseExited(MouseEvent e) {
            }
        });

        //metodo para adicionar evento a tabela, TableModelListener é uma das maneiras de ouvir
        //o que foi alterado em uma tabela.
        dtm.addTableModelListener(new TableModelListener() {

            @Override
            public void tableChanged(TableModelEvent e) {//verifica cada alteração feita na tabela
                System.out.println("Changed the last row is:  " + e.getLastRow()
                        + "Changed first row is: " + e.getFirstRow());
                System.out.println(e.getSource());

            }
        });

        jTable1.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {
                if (!e.getValueIsAdjusting()) {
                    System.out.println("The value changed is: " + e.toString());
                }
            }
        });
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setModalityType(java.awt.Dialog.ModalityType.DOCUMENT_MODAL);

        jTable1.setBorder(new javax.swing.border.MatteBorder(null));
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTable1.setRowSelectionAllowed(false);
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(213, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                TestSwing dialog = new TestSwing(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
}
